---
sidebar_position: 5
---

# Validate

## What is schema.Validate()?

To validate your data you can use the `schema.Validate()` function. The function signature looks like this:

```go
schema.Validate(dataPointer, options...)
```

This works with any Zog Schema:

```go
// string
data := "test"
z.String().Min(3).Validate(&data)
// structs
user := User{Name: "test"}
z.Struct(z.Shape{"name": z.String().Min(3)}).Validate(&user)
```

## Required and Zero Values

One quirk of `schema.Validate()` is that it will consider zero values as invalid if the schema is required. For example:

```go
var data int
z.Int().Required().Validate(&data) // Error: int is required
data = 0
z.Int().Required().Validate(&data) // Error: int is required
```

To fix this you can use a pointer:

```go
var data *int
z.Ptr(z.Int()).NotNil().Validate(&data) // will return a not nil issue
*data = 0
z.Ptr(z.Int()).NotNil().Validate(&data) // No issues will be returned
```

## Struct Validation Pattern

Some people prefer calling validate method on the struct themselves. For that you can use this:

```go
type User struct {
	ID   int
	Name string
}

var userSchema = z.Struct(z.Shape{
	"ID":   z.Int().Required(),
	"name": z.String().Required(),
})

func (u *User) Validate() z.ZogIssueMap {
	return userSchema.Validate(u)
}

// in some handler somewhere
user := User{ID: 1, Name: "Alice"}
err := user.Validate()

if err != nil {
	fmt.Println(err)
}
```
